<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :page.sync="page"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu="{ row }">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleUpdate(row)"
        >
          修改
        </el-button>
        <el-button
          v-if="row.userId !== 1"
          size="mini"
          type="text"
          icon="el-icon-delete"
          @click="handleDelete(row)"
        >
          删除
        </el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-key"
          @click="handleResetPwd(row)"
        >
          重置
        </el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-view"
          @click="handlePermitDetail(row)"
        >
          查看权限
        </el-button>
      </template>
    </avue-crud>
    <form-dialog ref="form" @submit_success="onLoad"></form-dialog>
    <permit-dialog ref="permit"></permit-dialog>
  </basic-container>
</template>

<script>
import { getPage, remove, edit } from "@/api/sys/user";
import { options } from "./options";
import Mixins from "@/mixins/mixins";
import FormDialog from "./form";
import PermitDialog from "./permitDialog";

export default {
  components: { FormDialog, PermitDialog },
  mixins: [Mixins],
  data() {
    return {
      form: {},
      option: options,
      remove,
      fileList: [],
      shipType: [],
    };
  },
  computed: {},
  methods: {
    beforeOpen(done, type) {
      if (["add"].includes(type)) {
        console.log("1111");
        this.handleAdd();
        // done();
      }
    },
    onLoad(page = this.page, params = {}) {
      this.loading = true;
      getPage(Object.assign(params, this.query, this.getPageParams(page))).then(
        ({ data }) => {
          this.data = data.records;
          this.page = this.getPageReturn(data);
          this.loading = false;
          this.selectionClear();
        }
      );
    },
    handleAdd() {
      this.$refs.form.open("add");
    },
    handleUpdate(row) {
      if (typeof row.roleIds === "string") row.roleIds = row.roleIds.split(",");
      this.$refs.form.open("edit", row);
    },
    handleResetPwd(row) {
      this.$prompt('请输入"' + row.username + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          row.password = value;
          edit(row).then(() => {
            this.$message.success("修改成功，新密码是：" + value);
          });
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.selectIds;
      this.$confirm(
        '是否确认删除用户名为"' + row.username + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return remove(userIds);
        })
        .then(() => {
          this.onLoad();
          this.msgSuccess("删除成功");
        });
    },
    handlePermitDetail(row) {
      this.$refs.permit.open(row);
    },
  },
};
</script>

<style></style>
